// =====================================================================================
// Copyright ?2006 SmartCodeGenerator created by Shahed Khan. 
/*
//Microsoft Shared Source Community License (MS-CL)
//Published: October 18, 2005
*/
// If you like this code then feel free to go ahead and use it.
// Your use of this software is entirely at your own risk.
// I make no claims or warrantees about the reliability or fitness of this code for any particular purpose.
// website www.smartcodegenerator.com, email shahed.khan@gmail.com
// =====================================================================================

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Diagnostics;

namespace SmartCodeGen.Providers
{
    [Serializable]
    public class TableSchema: TabularBase
    {
        // Methods
        public TableSchema(DatabaseSchema database, string name, string owner, DateTime dateCreated)
        {
            //this.a = DateTime.MinValue;
            base.connectionString = database.ConnectionString;
            base.databaseSchemaProvider = database.DatabaseSchemaProvider;
            base.database = database;
            base.ConnectionString = database.ConnectionString;
            base.name = name;
            this.owner = owner;
            this.dateCreated = dateCreated;

        }
        public TableSchema(DatabaseSchema database, string name, string owner, DateTime dateCreated, ExtendedPropertyCollection extendedProperties) :
            this(database,name,owner,dateCreated)
        {
            base.extendedProperties = extendedProperties;
        }

        public bool IsDependantOf(TableSchema table)
        {
            TableSchemaCollection collection1 = new TableSchemaCollection();
            return this.dependentOf(table, collection1);

        }

        private bool dependentOf(TableSchema schema2, TableSchemaCollection collection1)
        {
            for (int num1 = 0; num1 < schema2.ForeignKeys.Count; num1++)
            {
                TableSchema schema1 = schema2.ForeignKeys[num1].PrimaryKeyTable;
                if (schema1.Equals(this))
                {
                    return true;
                }
                if (!collection1.Contains(schema1))
                {
                    collection1.Add(schema1);
                    if (this.dependentOf(schema1, collection1))
                    {
                        return true;
                    }
                }
            }
            return false;
        }


        
        //public override string ToString( )
        //{
        //    throw new NotImplementedException();
        //}

      // Properties

        ColumnSchemaCollection columns;
        public ColumnSchemaCollection Columns
        {
            get
            {
                if (this.columns == null)
                {
                    //this.Database.c();
                    ColumnSchemaCollection collection1 = DBSchema.GetTableColumns(this);
                    if (collection1 != null)
                    {
                        //this.columns = ColumnSchemaCollection.ReadOnly(collection1);
                        this.columns = collection1;
                    }
                }
                return this.columns;

            }
        }

        private DateTime dateCreated;
        public DateTime DateCreated
        {
            get
            {
                return this.dateCreated;
            }
        }

        TableKeySchemaCollection foreignKeys;
        public TableKeySchemaCollection ForeignKeys
        {
            get
            {
                if (this.foreignKeys == null)
                {
                    try
                    {
                        TableKeySchemaCollection collection1 = new TableKeySchemaCollection();
                        foreach (TableKeySchema schema1 in this.Keys)
                        {
                            if (schema1.ForeignKeyTable == this)
                            {
                                collection1.Add(schema1);
                            }
                        }
                        this.foreignKeys = collection1;
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex);
                    }
                }
                return this.foreignKeys;

            }
        }

        //public string FullName
        //{
        //    get
        //    {
        //        throw new NotImplementedException();
        //    }
        //}


        IndexSchemaCollection indexes;
        public IndexSchemaCollection Indexes
        {
            get
            {
                if (this.indexes == null)
                {
                    //this.Database.c();
                    IndexSchemaCollection collection1 = DBSchema.GetTableIndexes(this);
                    if (collection1 != null)
                    {
                        //this.indexes = IndexSchemaCollection.ReadOnly(collection1);
                        this.indexes = collection1;
                    }
                }
                return this.indexes;

            }
        }


        TableKeySchemaCollection keys;
        public TableKeySchemaCollection Keys
        {
            get
            {
                if (this.keys == null)
                {
                    //this.Database.c();
                    TableKeySchemaCollection collection1 = DBSchema.GetTableKeys(this);
                    if (collection1 != null)
                    {
                        //this.keys = TableKeySchemaCollection.ReadOnly(collection1);
                        this.keys = collection1;
                    }
                }
                return this.keys;

            }
        }

        private ColumnSchemaCollection nonKeyColumns;
        public ColumnSchemaCollection NonKeyColumns
        {
            get
            {
                if (this.nonKeyColumns == null)
                {
                    ColumnSchemaCollection collection1 = new ColumnSchemaCollection();
                    foreach (ColumnSchema schema1 in this.Columns)
                    {
                        if (!schema1.IsPrimaryKeyMember && !schema1.IsForeignKeyMember)
                        {
                            collection1.Add(schema1);
                        }
                    }
                    //this.nonKeyColumns = ColumnSchemaCollection.ReadOnly(collection1);
                    this.nonKeyColumns = collection1;
                }
                return this.nonKeyColumns;

            }
        }

        private ColumnSchemaCollection nonPrimaryKeyColumns;
        public ColumnSchemaCollection NonPrimaryKeyColumns
        {
            get
            {
                if (this.nonPrimaryKeyColumns == null)
                {
                    ColumnSchemaCollection collection1 = new ColumnSchemaCollection();
                    foreach (ColumnSchema schema1 in this.Columns)
                    {
                        if (!schema1.IsPrimaryKeyMember)
                        {
                            collection1.Add(schema1);
                        }
                    }
                    //this.nonPrimaryKeyColumns = ColumnSchemaCollection.ReadOnly(collection1);
                    this.nonPrimaryKeyColumns = collection1;
                }
                return this.nonPrimaryKeyColumns;

            }
        }

        private string owner;
        public string Owner
        {
            get
            {
                return this.owner;
            }
        }

        PrimaryKeySchema primaryKey;
        public PrimaryKeySchema PrimaryKey
        {
            get
            {
                if (this.primaryKey == null)
                {
                    try
                    {
                        //this.Database.c();
                        PrimaryKeySchema schema1 = DBSchema.GetTablePrimaryKey(this);
                        if (schema1 != null)
                        {
                            this.primaryKey = schema1;
                        }
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex);
                    }
                }
                return this.primaryKey;
            }
        }

        TableKeySchemaCollection primaryKeys;

        public TableKeySchemaCollection PrimaryKeys
        {
            get
            {
                if (this.primaryKeys == null)
                {
                    try
                    {
                        TableKeySchemaCollection collection1 = new TableKeySchemaCollection();
                        foreach (TableKeySchema schema1 in this.Keys)
                        {
                            if (schema1.ForeignKeyTable != this)
                            {
                                collection1.Add(schema1);
                            }
                        }
                        //this.primaryKeys = TableKeySchemaCollection.ReadOnly(collection1);
                        this.primaryKeys = collection1;
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex);
                    }
                }
                return this.primaryKeys;
            }
        }


        #region Custom Properties for convenience               

        
        public string ClassName
        {
            get { return SchemaHelper.GetClassName(this); }
        }

        public string AbstractClassName
        {
            get { return SchemaHelper.GetAbstractClassName(this); }
        }
        

        #endregion
    }
}
